Javascript is now better than Python
=programming
Every year, there are millions of
people who decide to try programming, and, not knowing much about software,
ask:
"What programming language should I learn?"
Python and
Javascript are common answers to that question. Javascript can work on web
pages, while Python has been considered a language that's easy for beginners
to learn. But I've concluded that Python is now never the correct choice for
programming, except in these 2 cases:
- You need a
specific Python library.
- You're doing a leetcode interview.
Python is slow
Python is
much slower than Javascript, because its basic design has features that are
hard for interpreters to optimize. Notably, calling a function in Python is
over 10x as expensive as in most other languages. There are multiple
reasons, but the biggest one is: monkey-patching means the function must be
looked up at every call, even in a loop.
I've seen some proposed uses
for monkey-patching, such as: You wrote a bunch of code calling a function
from a library, and now you want to change what that function does without
changing the library (which might need automatic updates later). In other
words, the purpose is making code do something other than what it
says it does. For the same reasons that those are hard for
interpreters to reason about, they're also hard for humans reading the code
to reason about. Personally, I like to be able to read code and tell what it
does.
Monkey-patching was a mistake, and Python was designed around
making it easy because Guido von Rossum has bad taste.
Python is bad for complex projects
There are 2 main reasons why I consider Python unsuitable for large or
complex programs:
variable scoping
In most
modern languages, a variable is created with constructs like "let x = ..."
and then changed with "x = ..." but in Python both of those are the same.
That makes variable scoping unclear. It's not clear when reading code
whether an existing variable is being modified, and people can accidentally
modify an existing variable when they happen to choose an existing variable
name.
types
I consider type
systems to be essential for making reliable large software projects. And of
course, a good type system is better than one with only primitive types like
integers and floats. Python technically has some types, but they're so
minimal as to be almost useless.
Python is hard to distribute
With Python, if you want to send something to users that reliably works,
it's been rather difficult.
Many times, I've downloaded a Python
project, installed the dependencies, and then...the dependencies didn't
work. I'd go
so far as to say that Python specifically is largely responsible for the
rise of Docker - with a sane programming language you can just make a truly
standalone binary and not have to include a whole OS around it. Apparently
"uv" has recently made the situation a
bit better, but it's been a problem for a long time, and meanwhile npm
packages have just worked.
It's also hard to package standalone
Python programs for users. Javascript, on the other hand, has many workable
options for that:
- put your
program online and let people use it as a web app
- start a local server
and open the default browser
- use Tauri to use the installed WebView
- include a whole browser with your program (Electron)
obligatory whitespace mention
Every post about Python has to say something about indentation, but it
seemed like everything about that was already discussed ad nauseum. Luckily,
I recently learned why Python standardized on spaces instead of tabs!
When Guido was asked why PEP-8 says all lines of code should 79 chars or
less, he
replied:
Because an 80 char wide Emacs window starts wrapping at 79.
And tabs annoyed him because
people using different tab widths - especially Javascript users with a tab
width of 2 - would write code that, with a tab width of 4, would go past 79
characters.
This is an example of Guido van Rossum having the wrong
priorities because he has bad taste. With wide monitors being cheap, always
limiting line width to 79 characters is objectively wrong, as Linus
Torvalds noted.
why Python succeeded
Per
the above, Python is not very good for software that's
performance-sensitive, complex, large, or distributed to non-developers.
Since most software falls in one of those categories, it raises the question
of why Python has been popular.
leetcode
Python is
particularly suitable for leetcode interviews because:
- It's always
an option, and something less-popular might not be.
- Many interviews
don't allow installing extra libraries, because "you're supposed to use
basic tools to show your low-level understanding". Python has a relatively
large default library which is obviously an advantage in that case.
-
It's garbage collected.
- Leetcode interviews don't involve working with
a large existing system, in which case having type information would be more
helpful.
- Leetcode interviews generally don't care about performance
constant factors.
Since software companies have hired largely based on such interviews, their hiring has favored Python users more than it should have.
memes to libraries
Since
Python has been considered "the language that's easy for beginners", library
developers who want to make things that beginners can use have often picked
Python. Then, having easy-to-use libraries helped Python maintain that
reputation, more so than the language itself.
google
Google is one of the
most valuable and successful companies to ever exist. Some early hires at
Google happened to like Python. Google was also an early company to heavily
hire based on leetcode interviews. Once Google became successful, its use of
both Python and leetcode interviews was blindly copied by the management of
many other companies.
Google, of course, makes most of its money from
advertising, and collects personal data aggressively. This reminds me: I
recommend using either Ungoogled Chromium or Firefox as your browser, and
the Ublock Origin extension (which doesn't work on Chrome). Not for
ideological reasons, but because it will make your life a little better.
falling behind
Javascript,
C#, and Rust have all improved a lot over the past 10 years. Python has,
compared to those, made little progress.
Javascript got better
Some
ways Javascript improved more than Python include:
Faster compilers.
Companies wanted their browser to be popular, and Javascript performance was
directly linked to browser popularity, so companies put an enormous (for
compilers) amount of money into making Javascript run faster.
Javascript only added let and const in
2015. Yes, that was 10 years ago, but it was 20 years after
the languages was introduced. 20 years of people using var
for everything, and now it's rarely seen.
Optional chaining with x?.y
was introduced in 2020, and now you see it used constantly.
TypeScript now exists. It provides Javascript with a more sophisticated
type system than most typed languages have. It got significant improvements
in 2014 and 2016, and it's now common for Javascript libraries to provide
types.
npm was first released in 2010. It made some improvements over
time, like adding package-lock.json in 2017. It has a lot of libraries now.
npm dependencies
The
existence of npm is both a blessing and a curse - a blessing because it's
easy to use, and a curse because library authors use it too much. Because of
that, Javascript
has
~3x the average dependencies per project of Go, and ~7x the dependency count
of Python.
It was obvious to me that supply-chain attacks were a
security problem. Now they're happening regularly, and companies have
finally started to be aware of that risk. I recommend the following
mitigations:
- Avoid using libraries with many dependencies.
- Use
pnpm, or disable npm install scripts with "npm config set ignore-scripts
true".
- Use pnpm, and set minimumReleaseAge.
conclusion
Python deserves
to go in the dustbin of history alongside COBOL, PHP, and Matlab. I still
have to use it sometimes, but I'd prefer not to.